<!-- 商品发布 - 商品协议 -->
<template>
  <el-form ref="formRef" :model="formData" :rules="rules" label-width="130px" :disabled="isDetail">
    <div
      v-if="formData.salesChannel?.includes(2)"
      class="p-10px border-rd-5px mb-20px border-1px flex flex-items-center bg-[var(--el-color-primary-light-9)] font-size-14px"
    >
      <span>商品销售合约设置</span>
    </div>
    <el-form-item label="商品协议" prop="protocolId">
      <el-select v-model="formData.protocolId" class="w-80!" placeholder="请选择商品协议">
        <el-option
          v-for="item in protocolList"
          :key="item.id"
          :label="item.name"
          :value="item.id"
        />
      </el-select>
    </el-form-item>
  </el-form>
</template>
<script lang="ts" setup>
import type { Spu } from '@/api/mall/product/spu'
import { PropType } from 'vue'
import { propTypes } from '@/utils/propTypes'
import { copyValueToTarget } from '@/utils'
import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'

defineOptions({ name: 'ProductPermissionForm' })

const message = useMessage() // 消息弹窗

const props = defineProps({
  propFormData: {
    type: Object as PropType<Spu>,
    default: () => {}
  },
  activeName: propTypes.string.def(''),
  isDetail: propTypes.bool.def(false) // 是否作为详情组件
})

const formRef = ref() // 表单Ref
// 表单数据
const formData = ref<Spu>({
  promiseList: [], //服务承诺
  memberIntegral: 0, //会员积分设置
  paidmemberIntegral: 0, //付费会员积分设置
  memberIntegralDeduction: 0,
  paidmemberIntegralDeduction: 0,
  memberBalanceDeduction: 0,
  paidmemberBalanceDeduction: 0,
  voucherSelectId: 0
})
// 表单规则
const rules = reactive({})

/** 将传进来的值赋值给 formData */
watch(
  () => props.propFormData,
  (data) => {
    if (!data) {
      return
    }
    copyValueToTarget(formData.value, data)
  },
  {
    immediate: true
  }
)

/** 表单校验 */
const emit = defineEmits(['update:activeName'])
const validate = async () => {
  if (!formRef) return
  try {
    await unref(formRef)?.validate()
    // 校验通过更新数据
    Object.assign(props.propFormData, formData.value)
  } catch (e) {
    message.error('【其它设置】不完善，请填写相关信息')
    emit('update:activeName', 'other')
    throw e // 目的截断之后的校验
  }
}
defineExpose({ validate })
/** 初始化 */
const protocolList = ref<[]>([]) // 商品协议列表
onMounted(async () => {
  // 获取商品品牌列表
  protocolList.value = []
  // 运费模版
})
</script>
